﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Xps.Packaging;
using System.IO;

namespace DocManagerControls.Controls
{
    /// <summary>
    /// Interaction logic for DocViewControl.xaml
    /// </summary>
    public partial class DocViewControl : UserControl
    {
        public DocViewControl()
        {
            InitializeComponent();
        }

        protected void showDoc(string pDocumentPath)
        {
            FlowDocumentPageViewer vv = new FlowDocumentPageViewer();

            FlowDocument flowDocument = new FlowDocument();

            using (FileStream fileStream = File.Open(pDocumentPath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                //Paragraph para = new Paragraph();
                //para.Inlines.Add("Hello World!");
                //flowDocument.Blocks.Add(para);

                TextRange textRange = new TextRange(flowDocument.ContentStart, flowDocument.ContentEnd);

                var canLoadRTF = textRange.CanLoad(DataFormats.Rtf);
                textRange.Load(fileStream, DataFormats.Rtf);

                //para.Inlines.Add(textRange.Text); 
                textRange.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.Blue));
                textRange.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
            }

            vv.Document = flowDocument;
            this.Content = vv;

            //m_DocView.Document = vv.Document ;

            vv.Document = readDoc(pDocumentPath).GetFixedDocumentSequence();// flowDocument;

        }//fM



        public XpsDocument readDoc(string pDocumentPath)
        {
            var flowDocument = new FlowDocument();

            var textRange = new TextRange(flowDocument.ContentStart, flowDocument.ContentEnd);

            using (FileStream fileStream = File.Open(pDocumentPath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                textRange.Load(fileStream, DataFormats.Text);
            }

            var xpsDocument = new XpsDocument(@"c:\prova_Ok.xps", FileAccess.ReadWrite);
            var xpsDocumentWriter = XpsDocument.CreateXpsDocumentWriter(xpsDocument);
            xpsDocumentWriter.Write(((IDocumentPaginatorSource)flowDocument).DocumentPaginator);
            xpsDocument.Close();
            return xpsDocument;

        }//fm



    }//fC
}
